package de.fhtrier.gdw2.ships.menu.pages;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import de.fhtrier.gdw2.ships.SpaceshipGame;
import de.fhtrier.gdw2.ships.menu.MenuManager;
import de.fhtrier.gdw2.ships.menu.MenuPage;
import de.fhtrier.gdw2.ships.menu.MenuPageAction;
import de.fhtrier.gdw2.ships.menu.base.IActionListener;
import de.fhtrier.gdw2.ships.menu.base.Widget;
import de.fhtrier.gdw2.ships.states.GameplayState;
import de.fhtrier.gdw2.ships.utils.AssetLoader;

/**
 * Menu page: Root Menu
 * 
 * @author Lusito
 * @todo file paths need to be adapted when resource loader is ready.
 */
public class MenuPageRoot extends MenuPage {
	protected List<Widget> gears = new ArrayList<>();
	
	public MenuPageRoot(final GameContainer container, final StateBasedGame _game, final MenuManager manager, final boolean ingame)
			throws SlickException {
		super(container, _game, manager, null, "root");

		Font font = AssetLoader.getInstance().getFont("verdana_46");
		
		float x = 50;
		float y = 400;
		float h = font.getLineHeight() * 1.2f;
		int i=2;
		
		if(!ingame) {
			addLeftAlignedButton("Singleplayer", x, y - h * (i--), font, new IActionListener() {

                @Override
                public void onAction() {
                    SpaceshipGame.changeState(SpaceshipGame.GAMESTATE);
                }
            });
            
			MenuPage create = new MenuPageConnection(container, _game, manager, this, "Erstellen", "Spiel erstellen", "createserver");
			addLeftAlignedButton("Spiel erstellen", x, y - h * (i--), font, new MenuPageAction(manager, create));
			MenuPage join = new MenuPageConnection(container, _game, manager, this, "Verbinden", "Spiel beitreten", "join");
			addLeftAlignedButton("Spiel beitreten", x, y - h * (i--), font, new MenuPageAction(manager, join));
		}
		else {
			addLeftAlignedButton("Fortsetzen", x, y - h * (i--), font, 
				new IActionListener() {
					public void onAction() {
						GameplayState.hideMenu();
					}
				});
			addLeftAlignedButton("Verbindung trennen", x, y - h * (i--), font, 
				new IActionListener() {
					public void onAction() {
						close();
						game.disconnect();
					}
				});
		}
        
		addCenteredButton("Exit", 840, y - h *(i), font,
			new IActionListener() {
				public void onAction() {
					System.exit(0); // todo
				}
			});
	}
}
